<?php namespace Phpcmf\Controllers\Member;

class Jindu extends \Phpcmf\Member\Module
{

	// 列表
	public function index() {
		$this->_Member_List();
	}

	public function add() {
		// get 参数 cid
		if (IS_POST) {
			if(!$this->member['is_admin']){
				$this->_json('0', '不是管理员无法添加进度条');
			}
			$cid = \Phpcmf\Service::L('input')->post('cid'); //圈子id
			$id = \Phpcmf\Service::L('input')->post('id'); //帖子id
			$title = \Phpcmf\Service::L('input')->post('title'); //标题
			$beizhu = \Phpcmf\Service::L('input')->post('beizhu'); //备注
			
			$quan_data = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $cid)->get()->getRowArray();
			$tie_data = \Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('id', $id)->get()->getRowArray();
			if(!$tie_data && $quan_data) {
				$this->_json('0', '没有数据无法添加进度条');
			}
			if(!$title && !$beizhu) {
				$this->_json('0', '标题或备注不能为空');
			}

			// 判断
			$jindu_data = \Phpcmf\Service::M()->db->table('1_quan_jindu_index')->where('tieid', $id)->where('quanid', $cid)->countAllResults();
			if($jindu_data){
				$data = [
					'code' => 1,
					'data' => '已有添加',
					'uid'=> 0,
				
				];
			} else {
				// 插入数据
				$insert_data = [
					'uid' => $this->uid,
					'quanid' => $cid,
					'tieid' => $id,
					'title' => $title,
					'beizhu' => $beizhu,
					'zhuangtai' => 0,
					'tianjiashijian' => time(),
					'kaishishijian' => 0,
					'jieshushijian' => 0,
				];
				$insert_id = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->insert($insert_data);
				
				$data = [
					'code' => 1,
					'data' => '添加成功',
					'uid'=> 0,
				
				];
			}
			
			
			$this->_json('', '添加成功',$data);
		} else {
			$data = [
				'code' => 0,
				'data' => '无效提交',
				'uid'=> 0,
			
			];
			$this->_json('0', '无效提交');
		}
		
	}

	public function edit() {
		if (IS_POST) {
			if(!$this->member['is_admin']){
				$this->_json('0', '不是管理员无法添加进度条');
			}
			$cid = \Phpcmf\Service::L('input')->post('cid'); //圈子id
			$id = \Phpcmf\Service::L('input')->post('id'); //帖子id
			$title = \Phpcmf\Service::L('input')->post('title'); //标题
			$beizhu = \Phpcmf\Service::L('input')->post('beizhu'); //备注
			$zhuangtai = (int)\Phpcmf\Service::L('input')->post('zhuangtai'); //状态
			$quan_data = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $cid)->get()->getRowArray();
			$tie_data = \Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('id', $id)->get()->getRowArray();
			if(!$tie_data && $quan_data) {
				$this->_json('0', '没有数据无法添加进度条');
			}
			if(!$title && !$beizhu) {
				$this->_json('0', '标题或备注不能为空');
			}
			// 判断
			$jindu_data = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $id)->where('quanid', $cid)->countAllResults();
			if($jindu_data){
				if($zhuangtai==0){
					$neirong = [
						'title' => $title,
						'beizhu' => $beizhu,
						'zhuangtai' => 0,
						'kaishishijian' => 0,
						'jieshushijian' => 0,
					];
				} elseif($zhuangtai==1){
					$neirong = [
						'title' => $title,
						'beizhu' => $beizhu,
						'zhuangtai' => 1,
						'kaishishijian' => time(),
						'jieshushijian' => 0,
					];
				} elseif($zhuangtai==2){
					$neirong = [
						'title' => $title,
						'beizhu' => $beizhu,
						'zhuangtai' => 2,
						'jieshushijian' => time(),
					];
					
				}
				
				// 修改帖子内容
				\Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $id)->where('quanid', $cid)->update($neirong);
				$data = [
					'code' => 1,
					'data' => '进度修改成功',
					'uid'=> 0,
				
				];
			} else {
				$data = [
					'code' => 0,
					'data' => '没有进度数据修改失败',
					'uid'=> 0,
				
				];
			}
			$this->_json('0', '没有进度数据修改失败',$data);
			
		} else {
			$this->_json('0', '无效提交');
		}
	}

	public function del() {
		$this->_Member_Del();
	}
	public function jindu_data_list() {
		$id = \Phpcmf\Service::L('input')->post('id'); //帖子id
		$jindu_data_list = \Phpcmf\Service::M()->table('1_quan_form_jindu_data')->where('tieid', $id)->getAll();
		$this->_json('1', '读取进度数据成功', $jindu_data_list);
	}
	//删除进度
	public function jindu_index_shanchu(){
		if(!$this->member['is_admin']){
			$this->_json('0', '不是管理员无法删除进度条');
		}
		$id = \Phpcmf\Service::L('input')->post('id'); //进度详情id
		// 判断
		$jindu_data = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $id)->countAllResults();

		if($jindu_data){
			// 删除进度详情
			\Phpcmf\Service::M()->db->table('1_quan_form_jindu_data')->where('tieid', $id)->delete();
			// 删除进度条
			\Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $id)->delete();
			$data = [
				'code' => 1,
				'data' => '进度删除成功',
				
			];
		} else {
			$data = [
				'code' => 0,
				'data' => '没有进度数据删除失败',
				
			];
		}
		$this->_json('0', '没有进度数据删除失败',$data);
	}
	
	//添加进度详情
	public function jindu_data_add() {
		if (IS_POST) {
			if(!$this->member['is_admin']){
				$this->_json('0', '不是管理员无法添加进度条');
			}
			$cid = \Phpcmf\Service::L('input')->post('cid'); //圈子id
			$id = \Phpcmf\Service::L('input')->post('id'); //帖子id
			$title = \Phpcmf\Service::L('input')->post('title'); //标题
			//$beizhu = \Phpcmf\Service::L('input')->post('beizhu'); //备注
			
			$quan_data = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $cid)->get()->getRowArray();
			$tie_data = \Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('id', $id)->get()->getRowArray();
			if(!$tie_data && $quan_data) {
				$this->_json('0', '没有数据无法添加进度条');
			}
			if(!$title) {
				$this->_json('0', '标题不能为空');
			}
				// 插入数据
				$insert_data = [
					'uid' => $this->uid,
					'quanid' => $cid,
					'tieid' => $id,
					'title' => $title,
					'beizhu' => '',
					'zhuangtai' => 0,
					'tianjiashijian' => 0,
					'kaishishijian' => 0,
					'jieshushijian' => 0,
				];
				$insert_id = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_data')->insert($insert_data);
				
				$data = [
					'code' => 1,
					'data' => '添加成功',
					'uid'=> 0,
				
				];
			
			
			
			$this->_json('', '添加成功',$data);
		} else {
			$data = [
				'code' => 0,
				'data' => '无效提交',
				'uid'=> 0,
			
			];
			$this->_json('0', '无效提交');
		}
	}
	public function jindu_data_edit() {
		if (IS_POST) {
			if(!$this->member['is_admin']){
				$this->_json('0', '不是管理员无法添加进度条');
			}
			$cid = \Phpcmf\Service::L('input')->post('cid'); //圈子id
			$id = \Phpcmf\Service::L('input')->post('id'); //进度详情id
			$tieid = \Phpcmf\Service::L('input')->post('tieid'); //帖子id
			$title = \Phpcmf\Service::L('input')->post('title'); //标题
			$index = \Phpcmf\Service::L('input')->post('index'); //进度条状态

			
			$zhuangtai = (int)\Phpcmf\Service::L('input')->post('zhuangtai'); //状态
			
			
			if(!$title) {
				$this->_json('0', '标题不能为空');
			}
			// 判断
			$jindu_data = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_data')->where('id', $id)->countAllResults();
			if($jindu_data){
				if($zhuangtai==0){
					$neirong = [
						'title' => $title,
						'zhuangtai' => 0,
						'kaishishijian' => 0,
						'jieshushijian' => 0,
					];
				} elseif($zhuangtai==1){
					$neirong = [
						'title' => $title,
						'zhuangtai' => 1,
						'kaishishijian' => time(),
						'jieshushijian' => 0,
					];
				} elseif($zhuangtai==2){
					$neirong = [
						'title' => $title,
						'zhuangtai' => 2,
						'jieshushijian' => time(),
					];
					$index_data = [
						'zhuangtai_data' => $index,
					];

					\Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $tieid)->update($index_data);
				}
				
				// 修改帖子内容
				\Phpcmf\Service::M()->db->table('1_quan_form_jindu_data')->where('id', $id)->update($neirong);
				$data = [
					'code' => 1,
					'data' => '进度修改成功',
					'uid'=> 0,
				
				];
			} else {
				$data = [
					'code' => 0,
					'data' => '没有进度数据修改失败',
					'uid'=> 0,
				
				];
			}
			$this->_json('0', '没有进度数据修改失败',$data);
			
		} else {
			$this->_json('0', '无效提交');
		}

	}
	//删除进度详情
	public function jindu_data_del() {
		if (IS_POST) {
			if(!$this->member['is_admin']){
				$this->_json('0', '不是管理员无法删除进度条');
			}
			$id = \Phpcmf\Service::L('input')->post('id'); //进度详情id
			// 判断
			$jindu_data = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_data')->where('id', $id)->countAllResults();
			if($jindu_data){
				// 删除进度详情
				\Phpcmf\Service::M()->db->table('1_quan_form_jindu_data')->where('id', $id)->delete();
				// 删除进度条
				//\Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $tieid)->delete();
				$data = [
					'code' => 1,
					'data' => '进度删除成功',
					'uid'=> 0,
				
				];
			} else {
				$data = [
					'code' => 0,
					'data' => '没有进度数据删除失败',
					'uid'=> 0,
				
				];
			}
			$this->_json('0', '没有进度数据删除失败',$data);
		}
	}


	
}
